package com.fenqing.leetcode.code.t2300_2400;

import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

/**
 * @author fenqing
 */
public class Topic2396 extends Topic {

    @Execute
    public boolean isStrictlyPalindromic(int n) {
        for (int i = 2; i < n - 1; i++) {
            if (!isPalindrome(binHexOct(n, i))){
                return false;
            }
        }
        return true;
    }

    public static boolean isPalindrome(String numStr){
        int p = 0, q = numStr.length() - 1;
        char[] chars = numStr.toCharArray();
        while (p < q) {
            if (chars[p] != chars[q]) {
                return false;
            }
            p++;
            q--;
        }
        return true;
    }

    public static String binHexOct(int num, int b){
        StringBuilder sb = new StringBuilder();
        while (num > 0) {
            sb.insert(0, num % b);
            num /= b;
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Topic2396 topic2396 = new Topic2396();
        boolean run = topic2396.run("9");
        System.out.println(run);
    }

}
